local AIO = AIO or require("AIO")
local LeoHandlers = AIO.AddHandlers("unbindsys", {})
local unbind_cost = {29434, 2}
local itemlink = GetItemLink(unbind_cost[1], 4)
local tips = {
"|cffff0000【提示】|r解绑失败，副本ID|cffff0000%d|r的绑定数据内未找到你的绑定信息！！",
"|cffff0000【提示】|r解绑失败，副本ID|cffff0000%d|r的是一个无效编号！！",
"|cffff0000【提示】|r解绑失败，请离开当前地图再进行尝试！！",
"|cff00ff00【提示】|r解绑成功，消耗|cff00ff00%d|r个%s解除了副本|cffffcc00[%s]|r的绑定状态！！",
"|cff00ff00【提示】|r解绑成功，消耗|cff00ff00%d|r个%s解除了所有副本的绑定状态！！",
"|cffff0000【提示】|r解绑失败，所持%s不足|cffff0000%d|r个！！"
}

function LeoHandlers.OnStaticPopupDialogsAccpet(player, instanceID, instanceName, instanceDifficulty)
	if (player:HasItem(unbind_cost[1], unbind_cost[2], false) == false) then
		player:SendBroadcastMessage(string.format(tips[6], itemlink, unbind_cost[2]))
		return
	end
	if (instanceName == "ALL") then
		player:UnbindAllInstances()
		player:SendBroadcastMessage(string.format(tips[5], unbind_cost[2], itemlink, instanceName))
		return
	end
	local Q = CharDBQuery("SELECT * FROM character_instance WHERE guid ="..player:GetGUIDLow().." AND instance ="..instanceID.."")
	if (Q == nil) then
		player:SendBroadcastMessage(string.format(tips[1], instanceID))
		return
	end
	Q = CharDBQuery("SELECT map FROM instance WHERE id ="..instanceID.." AND difficulty ="..instanceDifficulty.."")
	if (Q == nil) then
		player:SendBroadcastMessage(string.format(tips[2], instanceID))
		return
	end
	local mapid = Q:GetUInt32(0)
	if (player:GetMapId() == mapid) then
		player:SendBroadcastMessage(tips[3])
		return
	end
	player:RemoveFromGroup() --移除队伍，防止未知错误
	player:UnbindInstance(mapid, instanceDifficulty)
	player:RemoveItem(unbind_cost[1], unbind_cost[2])
	player:SendBroadcastMessage(string.format(tips[4], unbind_cost[2], itemlink, instanceName))
	player:SaveToDB()
end